package pl.edu.ii.uj.battleships.server;
import java.io.*;
import java.net.*;

/**
* Server for Android Battleships game project
*/

public class Server extends Thread{
	public static final int port = 23232;
    boolean end = false;

	Server(){
		start();
	}
	
	public void run(){
		try{
			ServerSocket serverSocket = new ServerSocket(port);
			System.err.println("Server started on  " + serverSocket.getInetAddress().getHostAddress() + " : " + port);
        
			while (true) {
				Socket socket = serverSocket.accept();
				System.out.println("New client accepted");
				try {				
					Thread t = new ClientThread(socket,this);				
					System.out.println("ClientThread started...");
				} catch (Exception e) { 
					System.err.println("Unexpected exception:\n" + e); 
					e.printStackTrace();
					socket.close(); 
					break;
				}
			}
		} catch(Exception e) { 
			System.err.println("Unexpected exception:\n" + e); 
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		Server server = new Server();
	}
}